//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.kun.portal.webframe.po;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Collection;
import java.util.HashSet;

import java.util.Set;
import javax.persistence.*;
import com.kun.portal.webframe.util.Loaded;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
@Entity
@Table(
        name = "fw_user",
        indexes = {@Index(
                columnList = "user_name",
                unique = true
        ), @Index(
                columnList = "user_cn_name",
                unique = true
        ), @Index(
                columnList = "phone",
                unique = true
        ), @Index(
                columnList = "email",
                unique = true
        )}
)
@DynamicUpdate(true)
@Cacheable
public class FwUserPo implements UserDetails {

    @Id
    @GeneratedValue(
            generator = "user_id"
    )
    @GenericGenerator(
            name = "user_id",
            strategy = "uuid"
    )
    @Column(
            name = "user_id",
            length = 32,
            nullable = false,
            updatable = false
    )
    private String userId;
    @Column(
            name = "user_name",
            length = 100
//            ,updatable = false
    )
    private String userName;
    @Column(
            name = "user_cn_name",
            length = 200
    )
    private String userCnName;
    @Column(
            name = "user_pwd",
            length = 32
    )
    @JsonBackReference("user_pwd")
    private String userPwd;
    @Transient
    @JsonBackReference("newPwd")
    public String newPwd;
    @Column(
            name = "remark",
            length = 1024
    )
    private String remark;
    @Transient
    private String ipAdr;
    @Column(
            name = "phone",
            length = 50
    )

    private String phone;




    @Column(
            name = "service_time"
    )
    private Integer serviceTime=0;

    @Column(
            name = "city_id",
            length = 11
    )
    private String cityId;
    @Column(
            name = "email",
            length = 32
    )
    private String email;
    @Column(
            name = "face_img",
            length = 255
    )
    private String faceImg;
    @Column(
            name = "user_status",
            length = 1
    )
    private String userStatus = "1";
    @Column(
            name = "creator",
            length = 32
    )
    private String creator;
    @Column(
            name = "create_time",
            length = 20
    )
    private String createTime;
    @Column(
            name = "modify_time",
            length = 20
    )
    private String modifyTime;
    @ManyToMany(
            fetch = FetchType.LAZY
    )
    @JoinTable(
            name = "fw_role_user",
            joinColumns = {@JoinColumn(
                    name = "user_id",
                    referencedColumnName = "user_id"
            )},
            inverseJoinColumns = {@JoinColumn(
                    name = "role_id",
                    referencedColumnName = "role_id"
            )}
    )
    @Cache(
            usage = CacheConcurrencyStrategy.READ_WRITE
    )
    private Set<FwRolePo> roles = new HashSet();
//    @ManyToMany(
//            fetch = FetchType.LAZY
//    )
//    @JoinTable(
//            name = "fw_team_user",
//            inverseJoinColumns = {@JoinColumn(
//                    name = "team_id",
//                    referencedColumnName = "team_id"
//            )},
//            joinColumns = {@JoinColumn(
//                    name = "user_id",
//                    referencedColumnName = "user_id"
//            )}
//    )
//    @Cache(
//            usage = CacheConcurrencyStrategy.READ_WRITE
//    )
//    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
//    private Set<FwTeamPo> teams = new HashSet();


    public FwUserPo() {
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserId() {
        return this.userId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserCnName(String userCnName) {
        this.userCnName = userCnName;
    }

    public String getUserCnName() {
        return this.userCnName;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public String getUserPwd() {
        return this.userPwd;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public String getCreator() {
        return this.creator;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getRemark() {
        return this.remark;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone() {
        return this.phone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return this.email;
    }

    public void setFaceImg(String faceImg) {
        this.faceImg = faceImg;
    }

    public String getFaceImg() {
        return this.faceImg;
    }

    public String getUserStatus() {
        return this.userStatus;
    }

    public void setUserStatus(String userStatus) {
        this.userStatus = userStatus;
    }

    public String getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getModifyTime() {
        return this.modifyTime;
    }

    public void setModifyTime(String modifyTime) {
        this.modifyTime = modifyTime;
    }

    public String getIpAdr() {
        return this.ipAdr;
    }

    public void setIpAdr(String ipAdr) {
        this.ipAdr = ipAdr;
    }

    public Set<FwRolePo> getRoles() {

        return (Set<FwRolePo>) Loaded.loaded(this.roles);
    }

    public Set<FwRolePo> addRole(FwRolePo role) {
        this.roles.add(role);
        return this.roles;
    }

    public Set<FwRolePo> deleteRole(FwRolePo role) {
        this.roles.remove(role);
        return this.roles;
    }

    public void setRoles(Set<FwRolePo> roles) {
        this.roles = roles;
    }

    public Set<FwTeamPo> getTeams() {
//        return (Set<FwTeamPo>) Loaded.loaded(this.teams);
        return null;
    }

    public void setTeams(Set<FwTeamPo> teams) {
       // this.teams = teams;
    }

    public Set<FwTeamPo> addTeam(FwTeamPo team) {
//        this.teams.add(team);
//        return this.teams;
        return null;
    }

    public Set<FwTeamPo> deleteTeam(FwTeamPo team) {
//        this.teams.remove(team);
//        return this.teams;
        return null;
    }

    public String getNewPwd() {
        return this.newPwd == null?this.userPwd:this.newPwd;
    }

    public void setNewPwd(String newPwd) {
        this.newPwd = newPwd;
    }

    public String getCityId() {
        return this.cityId;
    }

    public void setCityId(String cityId) {
        this.cityId = cityId;
    }

    public int hashCode() {

        int result = 1;
        result = 31 * result + (this.userId == null?0:this.userId.hashCode());
        return result;
    }

    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        } else if(obj == null) {
            return false;
        } else if(this.getClass() != obj.getClass()) {
            return false;
        } else {
            FwUserPo other = (FwUserPo)obj;
            if(this.userId == null) {
                if(other.userId != null) {
                    return false;
                }
            } else if(!this.userId.equals(other.userId)) {
                return false;
            }

            return true;
        }
    }

    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        List<String> roleIdList = (List)this.roles.stream().map((role) -> {
//            return "ROLE_" + role.getRoleCode();
//        }).collect(Collectors.toList());
//        String rolesStr = StringUtils.collectionToCommaDelimitedString(roleIdList);
        return AuthorityUtils.commaSeparatedStringToAuthorityList("123");
    }

    @JsonIgnore
    public String getPassword() {
        return this.getUserPwd();
    }

    @JsonIgnore
    public String getUsername() {
        return this.getUserName();
    }

    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return "1".equals(this.getUserStatus());
    }

    public Integer getServiceTime() {
        if(serviceTime==null){
            serviceTime=0;
        }
        return serviceTime;
    }

    public void setServiceTime(Integer serviceTime) {
        this.serviceTime = serviceTime;
    }
}
